package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowCatalogueService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowStatisticsService;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowProcessInstanceTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.FlowTaskTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.ProcessInstanceStatisticsQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.ProcessSummaryQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.ProcessSummaryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.TaskStatisticsQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessInstanceTraceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessSummaryRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskTraceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Service - 统计
 *
 * @author lilh
 * @date 2019-08-26 14:04
 */
@Service
public class FlowStatisticsServiceImpl implements FlowStatisticsService {

    @Autowired
    private FlowTaskTraceRepository flowTaskTraceRepository;

    @Autowired
    private FlowProcessInstanceTraceRepository flowProcessInstanceTraceRepository;

    @Autowired
    private FlowProcessSummaryRepository flowProcessSummaryRepository;

    @Autowired
    private FlowCatalogueService catalogueService;

    @Override
    public List<FlowTaskTraceVo> listTask(TaskStatisticsQueryVo taskStatisticsQueryVo) {
        PageHelper.startPage(taskStatisticsQueryVo.getPageNum(), taskStatisticsQueryVo.getPageSize());
        return flowTaskTraceRepository.listTask(taskStatisticsQueryVo);
    }

    @Override
    public List<FlowProcessInstanceTraceVo> listProcessInstance(ProcessInstanceStatisticsQueryVo processInstanceStatisticsQueryVo) {
        PageHelper.startPage(processInstanceStatisticsQueryVo.getPageNum(), processInstanceStatisticsQueryVo.getPageSize());
        return flowProcessInstanceTraceRepository.getProcessInstanceTraces(processInstanceStatisticsQueryVo);
    }

    @Override
    public List<ProcessSummaryVo> taskSummary(ProcessSummaryQueryVo processSummaryQueryVo) {
        if (catalogueService.checkTenantAuthorize(processSummaryQueryVo)) {
            return Collections.emptyList();
        }
        PageHelper.startPage(processSummaryQueryVo.getPageNum(), processSummaryQueryVo.getPageSize());
        return flowProcessSummaryRepository.taskSummary(processSummaryQueryVo);
    }

    @Override
    public List<ProcessSummaryVo> processInstanceSummary(ProcessSummaryQueryVo processSummaryQueryVo) {
        if (catalogueService.checkTenantAuthorize(processSummaryQueryVo)) {
            return Collections.emptyList();
        }

        PageHelper.startPage(processSummaryQueryVo.getPageNum(), processSummaryQueryVo.getPageSize());
        return flowProcessSummaryRepository.processInstanceSummary(processSummaryQueryVo);
    }
}
